package com.aiden.cashbook.bill

import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.BaseAdapter
import com.aiden.cashbook.R
import com.aiden.cashbook.base.BaseFragment
import com.aiden.cashbook.base.BaseRecyclerViewAdapter
import com.aiden.cashbook.bean.Bill
import com.aiden.cashbook.bill.BillContract
import com.aiden.cashbook.bill.dagger.DaggerBillListFragmentComponent
import com.aiden.cashbook.extension.display
import com.aiden.cashbook.extension.getDrawableId
import com.aiden.cashbook.extension.toast
import com.aiden.cashbook.utils.LogUtils
import com.yanzhenjie.recyclerview.swipe.widget.DefaultLoadMoreView
import kotlinx.android.synthetic.main.fragment_bill.*
import kotlinx.android.synthetic.main.layout_bill_item.*
import javax.inject.Inject

class BillListFragment @Inject constructor() : BaseFragment(), BillContract.View {

    @Inject
    lateinit var mPresenter: BillPresenter

    private var mPage = 0
    private val mSize = 10
    private var mDate: String? = null
    private val mBillList = ArrayList<Bill>()
    private lateinit var mAdapter: BaseRecyclerViewAdapter<Bill>

    override fun initView(): View {
        return mInflater.inflate(R.layout.fragment_bill, null)
    }

    override fun initData() {
        DaggerBillListFragmentComponent.create().buildBillListPresenterComponent().build().inject(this)
        LogUtils.e("bill", "initData")

        mPresenter.takeView(this)

//        srl_bill.setOnRefreshListener {
//            mPage = 0
//            mPresenter.billList(1L, mPage, mSize)
//        }

        rv_bill.setAutoLoadMore(true)
        rv_bill.useDefaultLoadMore()
        rv_bill.setLoadMoreListener {
            if (mPage * mSize == mBillList.size) {
                mPresenter.billList(1L, ++mPage, mSize)
            }
        }

        mAdapter = BaseRecyclerViewAdapter(activity!!, mBillList, R.layout.layout_bill_item) { view: View, bill: Bill, position: Int ->
            if (mDate == null) {
                mDate = bill.date
                ll_date.visibility = View.VISIBLE
                tv_date.text = bill.date
            } else {
                if (mDate.equals(bill.date)) {
                    ll_date.visibility == View.GONE
                } else {
                    ll_date.visibility = View.VISIBLE
                    tv_date.text = bill.date
                }
            }
            with(bill) {
                activity?.let { iv_icon.display(it.getDrawableId(getUseType().image)) }
                when (bill.type) {
                    "expense" -> {
                        ll_expense.visibility = View.VISIBLE
                        ll_income.visibility = View.GONE
                        tv_expense_type.text = getUseType().name
                        tv_expense_money.text = money.toString()
                    }
                    "income" -> {
                        ll_income.visibility = View.VISIBLE
                        ll_expense.visibility = View.GONE
                        tv_income_type.text = getUseType().name
                        tv_income_money.text = money.toString()
                    }
                }
            }
        }
        rv_bill.layoutManager = LinearLayoutManager(activity)
        rv_bill.adapter = mAdapter
    }

    override fun setListener() {

    }

    override fun loadSuccess(bills: List<Bill>) {
        activity?.let { it.toast("bill list load successful") }
        if (mPage == 0) {
            mBillList.clear()
        }
        mBillList.addAll(bills)
        mAdapter.notifyDataSetChanged()
//        if (srl_bill.isRefreshing) {
//            srl_bill.isRefreshing = false
//        } else {
//            if (mBillList.size > 0 && mPage * mSize == mBillList.size) {
//                rv_bill.loadMoreFinish(false, true)
//            } else {
//                if (mBillList.size == 0) {
//                    rv_bill.loadMoreFinish(false, false)
//                } else {
//                    rv_bill.loadMoreFinish(true, false)
//                }
//            }
//        }
    }

    override fun loadFailure() {
        activity?.let { it.toast("bill list load failure") }
//        if (srl_bill.isRefreshing) {
//            srl_bill.isRefreshing = false
//        } else {
//            if (mBillList.size > 0 && mPage * mSize == mBillList.size) {
//                rv_bill.loadMoreFinish(false, true)
//            } else {
//                if (mBillList.size == 0) {
//                    rv_bill.loadMoreFinish(false, false)
//                } else {
//                    rv_bill.loadMoreFinish(true, false)
//                }
//            }
//        }
    }


}